package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.service.NursingProjectPlanService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingPlanImpl implements NursingPlanService {

    @Autowired
    private NursingProjectPlanService nursingProjectPlanService;

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Override
    public List<NursingPlanVo> getAll() {
        return nursingPlanMapper.getAll();
    }

    @Transactional
    @Override
    public void add(NursingPlanDto nursingPlanDto) {
        NursingPlan bean = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        bean.setStatus(0);
        bean.setCreateBy(UserThreadLocal.getMgtUserId());
        bean.setCreateTime(LocalDateTime.now());
        bean.setUpdateBy(UserThreadLocal.getMgtUserId());
        bean.setUpdateTime(LocalDateTime.now());
        nursingPlanMapper.add(bean);
        List<NursingProjectPlanDto> plans = nursingPlanDto.getProjectPlans();
        plans.stream().forEach(getProjectPlan ->
            getProjectPlan.setPlanId(bean.getId())
        );
        nursingProjectPlanService.addProjectPlans(plans);
    }

    @Override
    public PageResponse<NursingPlanVo> getByPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Page page = nursingPlanMapper.getByPage(name,status);
        return PageResponse.of(page,NursingPlanVo.class);
    }

    @Override
    public NursingPlanVo getById(Long id) {
        List<NursingProjectPlanVo> projectPlans = nursingProjectPlanService.getProjectPlans(id);
        NursingPlanVo plan = nursingPlanMapper.getById(id);
        plan.setProjectPlans(projectPlans);
        return plan;
    }

    //修改护理计划
    @Override
    public void updatePlan(NursingPlanVo nursingPlanVo,Integer id) {
        nursingPlanVo.setId(id.longValue());
        nursingPlanVo.setUpdateBy(UserThreadLocal.getMgtUserId());
        nursingPlanVo.setUpdateTime(LocalDateTime.now());
        nursingPlanMapper.updatePlan(nursingPlanVo);
        nursingProjectPlanService.deleteProjectPlans(id.longValue());
        nursingPlanVo.getProjectPlans().forEach(getProjectPlan ->{
            getProjectPlan.setPlanId(id);
            nursingProjectPlanService.updateProjectPlans(getProjectPlan);
        });

    }

    @Override
    public void deletePlan(Long id) {
        nursingProjectPlanMapper.deleteProjectPlans(id);
        nursingPlanMapper.deletePlan(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        nursingPlanMapper.updateStatus(id,status);
    }
}
















